package com.onemore.cc.common.base;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.onemore.cc.common.listener.LifeCycleListener;
import com.onemore.cc.common.manager.ActivityStackManager;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


/**
 * Create by lizihao
 * Desc: 基类Activity
 * 所有Activity可继承此Activity，统一处理Activity配置和初始化
 * Date: 2018/9/17
 */
public abstract class BaseActivity extends RxAppCompatActivity {
    protected Context mContext;

    protected LifeCycleListener mListener;

    public BaseFragment lastFragment;
    //获取Fragment管理器
    private FragmentManager manager;
    //key是Fragment类名 value是Fragment对应的容器的ID
    public Map<String,Integer> fragments =  new HashMap<>();
    //key是容器ID，value是上一个Fragment
    public Map<Integer,BaseFragment> lastFragments = new HashMap<>();
    //记录容器ID
    public ArrayList<Integer> lastcontainerIds =new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        //透明状态栏
//        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//        //透明导航栏
//        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
//        //沉寂式状态栏
//        Window window = getWindow();
//        int flag= WindowManager.LayoutParams.FLAG_FULLSCREEN;
//        //设置当前窗体为全屏显示
//        window.setFlags(flag, flag);
        super.onCreate(savedInstanceState);
        if (mListener != null) {
            mListener.onCreate(savedInstanceState);
        }
        ActivityStackManager.getManager().push(this);
        if (getLayoutId() != 0) {
            setContentView(getLayoutId());
        }

        mContext = this;
        initView();
        initData();
        setListener();
    }

    /**
     * 获取显示View的xml文件ID (布局文件)
     */
    protected abstract int getLayoutId();

    /**
     * 初始化View、控件
     */
    protected abstract void initView();

    /**
     * 初始化Data
     */

    protected abstract void initData();

    /**
     * 注册监听
     */
    protected abstract void setListener();

    /**
     * 获取控件ID
     */
    public <T extends View> T obtainView(int resId) {
        return (T) findViewById(resId);
    }

    /**
     * 设置生命周期回调函数
     *
     * @param listener
     */
    public void setOnLifeCycleListener(LifeCycleListener listener) {
        mListener = listener;
    }


    @Override
    protected void onStart() {
        super.onStart();
        if (mListener != null) {
            mListener.onStart();
        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        if (mListener != null) {
            mListener.onRestart();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mListener != null) {
            mListener.onResume();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mListener != null) {
            mListener.onPause();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (mListener != null) {
            mListener.onStop();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mListener != null) {
            mListener.onDestroy();
        }

        ActivityStackManager.getManager().remove(this);
    }


    /**
     * 该方法用于Fragment切换
     * @param containerId 容器ID
     * @param fragmentClass 要切换的Fragment 目标Fragment
     * @param bundle 跳转Fragment是要传递的参数
     * @param isReplace 是否替换当前Fragment true：replace替换 false :hind 隐藏
     * @param isBack 该Fragment是否要添加至回退栈
     */
    public void changFragment(int containerId, Class<? extends BaseFragment> fragmentClass, Bundle bundle, boolean isReplace, boolean isBack, @Nullable Class<? extends BaseFragment> fmName){

        //获取Fragment类名 下面会用到类名做Tag
        String fragmentName = fragmentClass.getSimpleName();
        manager = getSupportFragmentManager();
        //开启事务
        FragmentTransaction transaction = manager.beginTransaction();
        //通过Tag来查找Fragment  如果查找到返回的就是该Fragment对象 否则是null 代表该Fragment没有被创建过
        BaseFragment currentFragment = (BaseFragment) manager.findFragmentByTag(fragmentName);
        //如果Fragment为null 就通过Java动态代理创建对应的Fragment对象

//        if(currentFragment == null){
        try {
            //通过Java动态代理创建Fragment
            currentFragment = fragmentClass.newInstance();
            transaction.add(containerId,currentFragment,fragmentName);
            fragments.put(fragmentName,containerId);
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
//        }

        //isReplace 为true代表替换
        if(isReplace) {
            if(lastFragments.size() > 0){
                //如果上一个Fragment不等于null就隐藏
                if(lastFragment != null){
                    //从FragmentManager中把替换掉的Fragment移除
                    this.fragments.clear();
                    lastFragments.clear();
                    //避免存在过多的Fragmeng
                    transaction.remove(lastFragment);
                }
            }
            transaction.replace(containerId, currentFragment, fragmentName);

        } else {
            if(lastFragments.size() > 0){
                //取出上一个Fragment
                lastFragment = lastFragments.get(containerId);
                if (lastFragment != null)
                    transaction.hide(lastFragment);
            }

            //显示当前的Fragment
            transaction.show(currentFragment);

        }
        //bundle 不等于null代表要传递参数
        if(bundle != null) {
            currentFragment.setParams(bundle);

        }
        //isBack为true是添加至回退栈
        if (isBack){
            transaction.addToBackStack(fragmentName);
        }

        transaction.commit();
        //记录当前容器上一个Fragment
        lastFragments.put(containerId,currentFragment);
        lastcontainerIds.add(containerId);

    }
}
